import {createRouter, createWebHistory, RouteRecordRaw} from 'vue-router'
import Container from "@/layout/Container.vue";
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from "@/store/index";

const routes: Array<RouteRecordRaw> = [
    {
        path: '/login',
        component: () => import(/* webpackChunkName: "auth" */ '../views/login/Login.vue'),
        meta: {hidden: true, title: '登录'},
    },
    {
        path: '/',
        component: Container,
        redirect: '/dashboard',
        meta: {title: '首页', icon: 'menu'},
        children: [{
            path: 'dashboard',
            name: 'Dashboard',
            component: () => import(/* webpackChunkName: "about" */ '../views/Home.vue'),
            meta: {title: '首页', icon: 'menu'},
        }]
    },
    {
        path: '/hotel',
        name: 'hotel',
        redirect: '/hotel/lists',
        component: Container,
        meta: {title: '酒店管理'},
        children: [{
            path: 'lists',
            name: 'hotelLists',
            component: () => import(/* webpackChunkName: "hotel" */ '../views/hotel/Lists.vue'),
            meta: {title: '酒店列表', icon: 'menu'},

        }, {
            path: 'create',
            name: 'hotelCreate',
            component: () => import(/* webpackChunkName: "hotel" */ '../views/hotel/Create.vue'),
            meta: {title: '新增酒店', icon: 'menu'},
        }],
    },
    {
        path: '/about',
        name: 'About',
        redirect: '/about/about',
        component: Container,
        children: [{
            path: 'about',
            name: 'about',
            component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
            meta: {title: 'About', icon: 'menu'},
        }],
    }
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes,
})
router.beforeEach((to, from, next) => {
    NProgress.start()
    if (!to.meta.hidden) {
        const state: any = store.state
        if (state.app.tabs.length === 0) {
            for (let route of router.getRoutes()) {
                if (route.path === '/') {
                    store.commit("app/pushTab", route)
                    break;
                }
            }
        }
        store.commit("app/pushTab", to)
    }
    next()
    NProgress.done(true)

})

export default router
